﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using GestionPedago.Persistence.OPManager;
using System.Data.Objects;

namespace GestionPedago.EntityContext
{
    public partial class PlanningECFDetail : EntityObject, IEquatable<PlanningECFDetail>
    {

        public PlanningECFDetail MiseAJourPourPlanificationParPromotion()
        {
            try
            {
                if (this.PlanningECF.Promotion != null)
                {
                    EntityConnection.GetContext().Titre.Detach(this.PlanningECF.Promotion.Formation.Titre);
                    EntityConnection.GetContext().Formation.Detach(this.PlanningECF.Promotion.Formation);
                    EntityConnection.GetContext().Promotion.Detach(this.PlanningECF.Promotion);
                }

                if (this.PlanningECF.ECF != null)
                {
                    EntityConnection.GetContext().ECF.Detach(this.PlanningECF.ECF);
                }
                if (!EntityStateChecker.IsDetached(this.PlanningECF))
                {
                    EntityConnection.GetContext().PlanningECF.Detach(this.PlanningECF);
                    this.PlanningECF = null;
                }

                if (this.ContactENI != null)
                {
                    EntityConnection.GetContext().ContactENI.Detach(this.ContactENI);
                    this.ContactENI = null;
                }

                EntityConnection.GetContext().SaveChanges();
                return this;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public PlanningECFDetail MiseAJour(ECF e)
        {
            try
            {
                bool nouveauPlanningECF = false;
                if (this.PlanningECF.CodePlanningECF == 0)
                {
                    // EntityConnection.GetContext().Detach(e);
                    IQueryable<PlanningECF> query = EntityConnection.GetContext().PlanningECF;
                    query = query.Where(it => it.DateECF == this.PlanningECF.DateECF && it.ECF.IdECF == e.IdECF);
                    PlanningECF pe = query.FirstOrDefault();
                    if (pe == null)
                    {
                        DateTime dte = this.PlanningECF.DateECF;
                        EntityConnection.ClearEntities();
                        pe = new PlanningECF();
                        pe.DateECF = dte;
                        pe.ECF = e;
                        pe = PlanningECFOPManager.AjouterPlanningECF(pe);
                        nouveauPlanningECF = true;
                        this.PlanningECF = PlanningECFOPManager.GetPlanningECFByCode(pe.CodePlanningECF);
                    }
                    else
                    {

                        this.PlanningECF = pe;

                    }

                    //EntityConnection.ClearEntities();

                }
                else
                {

                    this.PlanningECF = PlanningECFOPManager.GetPlanningECFByCode(this.CodePlanningECF);

                }



                if (this.PlanningECF.Promotion != null)
                {
                    foreach (Cours c in this.PlanningECF.Promotion.Cours.ToList())
                    {
                        EntityConnection.GetContext().Cours.Detach(c);
                    }
                    EntityConnection.GetContext().Titre.Detach(this.PlanningECF.Promotion.Formation.Titre);
                    EntityConnection.GetContext().Formation.Detach(this.PlanningECF.Promotion.Formation);
                    EntityConnection.GetContext().Promotion.Detach(this.PlanningECF.Promotion);
                }

                EntityConnection.GetContext().ECF.Detach(this.PlanningECF.ECF);
                EntityConnection.GetContext().PlanningECF.Detach(this.PlanningECF);

                if (this.ContactENI != null)
                {
                    EntityConnection.GetContext().ContactENI.Detach(this.ContactENI);
                }

                if (this.PlanningIndividuelFormation != null && !EntityStateChecker.IsDetached(this.PlanningIndividuelFormation))
                {
                    EntityConnection.GetContext().PlanningIndividuelFormation.Detach(this.PlanningIndividuelFormation);
                }
                EntityConnection.GetContext().SaveChanges();
                if (nouveauPlanningECF)
                {
                    PlanningECFOPManager.SupprimerTousPlanningECFHorsPromotionIsoles(e.IdECF);
                }
                EntityConnection.ClearEntities();
                return this;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool Equals(PlanningECFDetail other)
        {
            return this.IdPlanningECFDetail == other.IdPlanningECFDetail;
        }
    }
}
